package com.example.tonjie.software_park_association;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static com.example.tonjie.software_park_association.R.id.account;

/**
 * Created by TonJie on 2017/5/1.
 */

public class LoginActivity extends Activity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    private static final String TAG = "LoginActivits";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_login_and_registration);
        pref = PreferenceManager.getDefaultSharedPreferences(this);

        init();
        Login_to_judge();
        if (pref.getBoolean("remember", false)) {
            String account = pref.getString("account", "");
            String password = pref.getString("password", "");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            Log.d("LoginActivits", "onCreate: succeed" + accountEdit + accountEdit);
        }
    }

    private void init() {
        accountEdit = (EditText) findViewById(account);
        passwordEdit = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
    }

    private void Login_to_judge() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                editor = pref.edit();
                if (account.equals("13531982270") && password.equals("123456")) {
                    editor.putString("account", account);
                    editor.putString("password", password);
                    editor.putBoolean("remember", true);
                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                    editor.clear();
                }
                editor.apply();
            }
        });
    }
}
